/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { X2DBeta } = require("../graphics/X2DInterface");
const { XButton } = require("./XButton");

class XSelect extends XButton {
    constructor(options) {
        super(options);
        this.list_ = [1, 2, 3];
        this.default_ = 1;
        this.onSelected_ = null;
        this.open_ = false;
        this.nameColor_ = 0xff20f020;
        this.backgroundColor_ = 0xff804050;
        this.tmpSelect_ = -1;

        if (options) {
            if (options.list) {
                this.list_ = options.list;
            }
            if (options.default) {
                this.default_ = options.default;
            }
            if (options.onSelected) {
                this.onSelected_ = options.onSelected;
            }
        }
    }
    ResetList(list, default_) {
        this.list_ = list;
        this.default_ = default_
    }
    onDraw(offx = 0, offy = 0) {
        let x = this.posX_ + offx
        let y = this.posY_ + offy
        let w = this.posW_
        let h = this.posH_

        X2DBeta.fillRect(x, y, w, h, this.backgroundColor_);
        X2DBeta.fillRect(x + w - 20, y, 20, h, this.backgroundColor_ + 0x202020);
        X2DBeta.drawText(this.default_, 18, x, y, 1, 1, 0, -1, -1, this.nameColor_);
        if (this.open_) {
            X2DBeta.fillRect(x, y + h, w, 20 * this.list_.length, 0xff202020)
            for (let i in this.list_) {
                if (i == this.tmpSelect_) {
                    X2DBeta.fillRect(x, y + h + i * 20, w, 20, 0x80000080)
                }
                if (this.list_[i] == this.default_) {
                    X2DBeta.fillRect(x, y + h + i * 20, w, 20, 0x80008000)
                }
                X2DBeta.drawText(this.list_[i], 18, x, y + h + i * 20, 1, 1, 0, -1, -1, this.nameColor_)
            }
            X2DBeta.drawText("v", 20, x + w - 10, y + h / 2, 1, 1, 180, -2, -2, 0xff00ff00);
        }
        else {
            X2DBeta.drawText("v", 20, x + w - 10, y + h / 2, 1, 1, 0, -2, -2, 0xff00ff00);
        }
    }
    IsTouchIn(x, y) {
        if (x < this.posX_) return false;
        if (y < this.posY_) return false;
        if (x > this.posX_ + this.posW_) return false;
        if (y > this.posY_ + this.posH_ + (this.open_ ? 20 * this.list_.length : 0)) return false;
        return true;
    }
    onTouch(msg, x, y) {
        let isIn = this.IsTouchIn(x, y);
        switch (msg) {
            case 1:
                if (isIn) {
                    if (!this.open_) this.open_ = true;
                    else {
                        if (this.tmpSelect_ >= 0 && this.tmpSelect_ <= this.list_.length) {
                            // if (this.default_ != this.list_[this.tmpSelect_]) {
                            if (this.onSelected_ != null) {
                                if (this.onSelected_(this.list_[this.tmpSelect_])) {
                                    this.default_ = this.list_[this.tmpSelect_];
                                }
                            }
                            // }
                        }
                        this.open_ = false;
                    }
                } else {
                    this.open_ = false;
                }
                break;
            case 2:
                if (this.open_) {
                    if (y - this.posY_ - this.posH_ < 0) this.tmpSelect_ = -1;
                    else this.tmpSelect_ = parseInt((y - this.posY_ - this.posH_) / 20)
                }
                break;
            case 3:
                break;
        }
        return isIn;
    }
}

module.exports = {
    XSelect
}